package com.open.capacity.reimburse.controller;

import java.util.Map;

import com.open.capacity.commons.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.BaseSupplier;
import com.open.capacity.reimburse.service.BaseSupplierService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 供应商
 *
 * @author
 * @email
 * @date 2023-10-31 15:39:05
 */
@RestController
@RequestMapping("basesuppliers")
@Api(tags = "供应商")
public class BaseSupplierController {

    @Autowired
    private BaseSupplierService baseSupplierService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('base_supplier:basesupplier:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = baseSupplierService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('base_supplier:basesupplier:list')")
    public Result findById(String id) {
        return Result.succeed(baseSupplierService.findById(id));
    }

    /**
     * 查询下一个code
     */
    @RequestMapping("/findNextCode")
    @PreAuthorize("hasAnyAuthority('base_supplier:basesupplier:list')")
    public Result findNextCode() {
        return Result.succeed(baseSupplierService.getNextCode(), "成功");
    }


    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('base_supplier:basesupplier:save')")
    public Result save(@RequestBody @Valid BaseSupplier baseSupplier, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            baseSupplierService.save(baseSupplier);
        } else {
            baseSupplier.setId(keyValue);
            baseSupplierService.update(baseSupplier);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('base_supplier:basesupplier:delete')")
    public Result delete(@PathVariable String id) {
        baseSupplierService.delete(id);
        return Result.succeed("删除成功");
    }

}
